﻿using AutoMapper;
using IOA.MES.Common;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOA.MES.Business
{
    /// <summary>
    /// 拼柜条件配置（错误代码：130001）
    /// </summary>
    public class CabinetConditionMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// 编辑
        /// </summary>
        public void Edit(VCabinetCondition model)
        {
            if (!string.IsNullOrWhiteSpace(model.WorkOrderNo))
            {
                var entities = mesDb.CabinetConditions.AsNoTracking().Where(_ => _.OrderNo == "" && _.WorkOrderNo == model.WorkOrderNo && _.QLevelCondition == model.QLevelCondition && _.Valid).ToList();
                foreach (var entity in entities)
                {
                    mesDb.CabinetConditions.Attach(entity);
                    entity.Valid = false;
                    entity.ModifyBy = model.CreateBy;
                    entity.ModifyTime = DateTime.Now;
                }

                model.OrderNo = string.Empty;
                model.CreateTime = DateTime.Now;
                var newEntity = Mapper.Map<CabinetCondition>(model);
                mesDb.CabinetConditions.Add(newEntity);
            }
            else if (!string.IsNullOrWhiteSpace(model.OrderNo))
            {
                var entities = mesDb.CabinetConditions.AsNoTracking().Where(_ => _.OrderNo == model.OrderNo && _.WorkOrderNo == "" && _.QLevelCondition == model.QLevelCondition && _.Valid).ToList();
                foreach (var entity in entities)
                {
                    mesDb.CabinetConditions.Attach(entity);
                    entity.Valid = false;
                    entity.ModifyBy = model.CreateBy;
                    entity.ModifyTime = DateTime.Now;
                }

                model.WorkOrderNo = string.Empty;
                model.CreateTime = DateTime.Now;
                var newEntity = Mapper.Map<CabinetCondition>(model);
                mesDb.CabinetConditions.Add(newEntity);
            }
            else
            {
                var entities = mesDb.CabinetConditions.AsNoTracking().Where(_ => _.OrderNo == "" && _.WorkOrderNo == "" && _.QLevelCondition == model.QLevelCondition && _.Valid).ToList();
                foreach (var entity in entities)
                {
                    mesDb.CabinetConditions.Attach(entity);
                    entity.Valid = false;
                    entity.ModifyBy = model.CreateBy;
                    entity.ModifyTime = DateTime.Now;
                }

                model.CreateTime = DateTime.Now;
                var newEntity = Mapper.Map<CabinetCondition>(model);
                mesDb.CabinetConditions.Add(newEntity);
            }
            mesDb.SaveChanges();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public VCabinetCondition GetCabinetCondition(string orderNo, string workOrderNo, QLevelCondition qLevelCondition)
        {
            CabinetCondition cabinetCondition = null;
            if (!string.IsNullOrWhiteSpace(workOrderNo))
            {
                var workOrder = mesDb.WorkOrders.AsNoTracking().FirstOrDefault(_ => _.WorkOrderNo == workOrderNo && _.Valid);
                if (workOrder == null)
                {
                    return null;
                }
                cabinetCondition = mesDb.CabinetConditions.AsNoTracking().FirstOrDefault(_ => _.QLevelCondition == qLevelCondition && _.OrderNo == "" && _.WorkOrderNo == workOrderNo && _.Valid);
                if (cabinetCondition == null)
                {
                    cabinetCondition = mesDb.CabinetConditions.AsNoTracking().FirstOrDefault(_ => _.QLevelCondition == qLevelCondition && _.OrderNo == workOrder.OrderNo && _.WorkOrderNo == "" && _.Valid);
                }
            }
            else if (!string.IsNullOrWhiteSpace(orderNo))
            {
                cabinetCondition = mesDb.CabinetConditions.AsNoTracking().FirstOrDefault(_ => _.QLevelCondition == qLevelCondition && _.OrderNo == orderNo && _.WorkOrderNo == "" && _.Valid);
            }
            else
            {
                cabinetCondition = mesDb.CabinetConditions.AsNoTracking().FirstOrDefault(_ => _.QLevelCondition == qLevelCondition && _.OrderNo == "" && _.WorkOrderNo == "" && _.Valid);
            }

            return Mapper.Map<VCabinetCondition>(cabinetCondition);
        }
    }
}
